package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.Manufacturer;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;
/**
 * 企业
 * @author Administrator
 *
 */
@Service
public class ManufacturerService extends BaseService<Manufacturer, String> {
	
	public List<TreeNode> getCompanyData(LoginUser loginUser){
		return getManufacturerData(null, loginUser);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<TreeNode> getManufacturerData(String groupid, LoginUser loginUser){
		List<TreeNode> list = new ArrayList<>();
		
		StringBuilder hql = new StringBuilder();
		hql.append("from Manufacturer c ");
		
		if(StringUtils.isNotEmpty(groupid)){
			hql.append(" where c.groupid like '"+groupid.replace("'", "")+"%'");
		}else {
			if(!SystemCfg.isAdminUser(loginUser) && !SystemCfg.isSystemAdminUser(loginUser)){
				String uroupid = loginUser.getGroupid();
				hql.append(" where c.groupid like '"+uroupid+"%'");
			}
		}
		
		hql.append(" order by c.companyName asc");
		List<Company> companies = listByHql(hql.toString());
		if(companies != null){
			for(Company company : companies){
				list.add(new TreeNode(company.getCompanyid(), company.getCompanyName(), false));
			}
		}
		return list;
	}
	
	/**
	 * 获取菜单树数据
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(LoginUser loginUser){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.Manufacturer(manufacturerid,manufacturerName)");
		hql.append(" from Manufacturer ");
		
		if(!SystemCfg.isSystemAdminUser(loginUser) && !SystemCfg.isAdminUser(loginUser)
				&& !"1".equals(loginUser.getUser_kind()) && !"2".equals(loginUser.getUser_kind())){
			hql.append(" where groupid like '"+loginUser.getGroupid()+"%' ");
		}
		
		hql.append(" order by manufacturerName asc");
		List<Manufacturer> manufacturers = listByHql(hql.toString());
		if(manufacturers != null){
			for(Manufacturer manufacturer : manufacturers){
				list.add(new JsTreeNode(manufacturer.getManufacturerid(),manufacturer.getManufacturerName(),"PRODUCTS",null,false));
			}
		}
		return list;
	}
	
	/**
	 * 列表数据
	 * @param materialid
	 * @param typeid
	 * @param materialName
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	
	public Pager list(String groupid, String search, String manufacturerid, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from Manufacturer c where 1=1 ");
		
		if(StringUtils.isEmpty(groupid) && !SystemCfg.isAdminUser(loginUser) && !SystemCfg.isSystemAdminUser(loginUser)){
			groupid = loginUser.getGroupid();
		}
		
		if(StringUtils.isNotEmpty(groupid)){
			groupid = groupid.replace("'", "");
			querier.append(" and groupid like :groupid ");
			querier.setParam("groupid", groupid+"%");
		}
		
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (manufacturerName like :manufacturerName)");
			querier.setParam("manufacturerName", search);
		}
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Manufacturer> listForApp(String search, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from Manufacturer c where 1=1 ");
		String groupid = loginUser.getGroupid();
		if(StringUtils.isNotEmpty(groupid)){
			groupid = groupid.replace("'", "");
			querier.append(" and groupid like :groupid ");
			querier.setParam("groupid", groupid+"%");
		}
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (manufacturerName like :manufacturerName)");
			querier.setParam("manufacturerName", "%"+search+"%");
		}
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		
		List<Manufacturer> list = listByQuerier(querier, pageParam);
		return list;
	}
	
	
	
	/**
	 * 添加企业信息
	 * @param material
	 * @param typeid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Manufacturer manufacturer, LoginUser loginUser, HttpServletRequest request){
		//System.out.println(company.getCompanyid());
		if(StringUtils.isNotEmpty(manufacturer.getImgurl())){
			UploadFile.moveTmpTo(manufacturer.getImgurl(), UploadFile.DIR_MANUFACTURER_IMG);
		}
		manufacturer.setCreateTime(new Date());
		manufacturer.setUpdateTime(new Date());
		manufacturer.setCreateBy(loginUser.getLogin_name());
		manufacturer.setUpdateBy(loginUser.getLogin_name());
		
		this.save(manufacturer);
		
		return null;
	}
	
	
	/**
	 * 修改企业信息
	 * @param company
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Manufacturer manufacturer, LoginUser loginUser, HttpServletRequest request){
		Manufacturer old = getSafeEntity(manufacturer.getManufacturerid());
		if(StringUtils.isNotEmpty(manufacturer.getImgurl()) && !manufacturer.getImgurl().equals(old.getImgurl())){
			UploadFile.moveTmpTo(manufacturer.getImgurl(), UploadFile.DIR_MANUFACTURER_IMG);
		}
		String fields = "manufacturerName,businessNo,corporatePerson,contactPerson,phone,address," +
				"manufacturerLevel,lat,lng,postcodes,productDesc,productivity,ccc,imgurl,remark";
		BeanUtil.setTargetFields(old, manufacturer, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<Manufacturer> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
		
	}
	
	
	/**
	 * 删除
	 * @param companyids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] ids, LoginUser loginUser, HttpServletRequest request){
		for(String manufacturerid : ids){
			Manufacturer manufacturer = get(manufacturerid);
			if(manufacturer==null){
				continue;
			}
			this.delete(manufacturer);
		}
		
		return null;
	}
	
	
	/**
	 * 判断指定名称是否被使用
	 * @param materialName
	 * @param materialid
	* @param typeid
	 * @return
	 */
	public boolean isNameDuplicate(String manufacturerName, String manufacturerid){
		if(StringUtils.isEmpty(manufacturerid)){
			Object obj = getByHql("select manufacturerid from Manufacturer where manufacturerName=? ", manufacturerName);
			return obj!=null;
		}else {
			Object obj = getByHql("select manufacturerid from Manufacturer where manufacturerid<>? and manufacturerName=? ", manufacturerid, manufacturerName);
			return obj!=null;
		}
	}
	
	
	
	/**
	 * 判断指定CODE是否被使用
	 * @param materialCode
	 * @param materialid
	 * @param typeid
	 * @return
	 */
	public boolean isCodeDuplicate(String manufacturerNo, String manufacturerid){
		if(StringUtils.isEmpty(manufacturerid)){
			Object obj = getByHql("select manufacturerid from Manufacturer where manufacturerNo=? ", manufacturerNo);
			return obj!=null;
		}else {
			Object obj = getByHql("select manufacturerid from Manufacturer where manufacturerid<>? and manufacturerNo=? ", manufacturerid, manufacturerNo);
			return obj!=null;
		}
	}
	
	public String getManufacturerNoMax(){
		String manufacturerNo = (String) getByHql("select max(manufacturerNo) from Manufacturer");
		return manufacturerNo;
	}
	
}
